package com.example.falvback.controller;

import com.example.falvback.common.Result;
import com.example.falvback.entity.Anli;
import com.example.falvback.service.AnliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 功能：
 * 作者：chen
 * 日期：2023/12/716:27
 */
@RestController
@RequestMapping("/anli")

public class AnliController {
    @Autowired
    AnliService anliService;//自动注入service层

    @GetMapping("/show")
    public Result show() {
        List<Anli> anliList = anliService.showAnli();
        return Result.success(anliList);
    }

    @PutMapping("/insert")
    public Result insert(@RequestBody Anli anli) {
        anliService.insert(anli);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getAnliById(@PathVariable Long id) {
        List<Anli> anli = anliService.findById(id);
        if (anli != null) {
            return Result.success(anli);
        } else {
            return Result.error("未找到指定ID的案例");
        }
    }
    @PutMapping("/update/{id}")
    public Result updateAnli(@PathVariable String id, @RequestBody Anli anli) {
        try {
            Integer anliId = Integer.parseInt(id);
            anli.setId(anliId); // Set the ID from the path variable
            anliService.updateAnli(anli);
            return Result.success();
        } catch (NumberFormatException e) {
            // 处理无效的 ID
            return Result.error("Invalid or undefined ID");
        }
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteAnli(@PathVariable Integer id) {
        try {
            anliService.deleteAnli(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除案例失败：" + e.getMessage());
        }
    }
//    @GetMapping("/parseWord")
//    public Result parseWordDocument(@RequestParam String url) {
//        try {
//            String parsedContent = wordService.parseWordDocument(url);
//            return Result.success(parsedContent);
//        } catch (IOException e) {
//            return Result.error("解析 Word 文档失败: " + e.getMessage());
//        }
//    }

    //根据栏目获取当前案例表内容。接口怎么写测试。
}
